package com.dinghui.pay.service.impl;

import com.dinghui.pay.dao.TbUserMapper;
import com.dinghui.pay.pojo.TbUser;
import com.dinghui.pay.pojo.param.LoginParam;
import com.dinghui.pay.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @author: trt
 * @description:
 * @create: 2021-05-30 10:34
 */
@Service
public class UserServiceImpl implements UserService {

    //@Autowired (required = false)
    @Resource
    private TbUserMapper userMapper;

    @Override
    public int getCount(TbUser user) {
        return userMapper.selectCount(user);
    }

    @Override
    public void save(TbUser user) {
        userMapper.insertSelective(user);
    }

    @Override
    public TbUser getOne(TbUser user) {
        return userMapper.selectOne(user);
    }

    @Override
    public long login(LoginParam param) throws Exception {
        TbUser tbUser = new TbUser();
        tbUser.setUsername(param.getUsername());
        TbUser user = userMapper.selectOne(tbUser);

        //密码错误
        if(!user.getPassword().equals(DigestUtils.sha256Hex(param.getPassword()))){
            throw new Exception("密码错误");
        }

        return user.getUserId();
    }
}